//This file contains material supporting section 10.9 of the textbook:
//"Object Oriented Software Engineering" and is issued under the open-source
//license found at www.lloseng.com

/*
* ClientFrame.java   2001-02-08
*
* Copyright (c) 2001 Robert Laganiere and Timothy C. Lethbridge.
* All Rights Reserved.
*
*/

package interface_client;

import interface_partie2.*;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.desanonymerUtilisateurs.UserList;
import com.drawpad.DrawPad;
import com.lloseng.ocsf.simplegui.client.SimpleClient;



/**
* The <code> ClientFrame </code> class is a simple interactive
* application made to exercise the OCSF framework.<p>
* Type <code>java ocsftester.ClientFrame host port_number</code> to start
* one client.<p>
* The window is
* pink when the connection has been closed, red
* when an exception is received,
* and green when connected to the server.
*
* @author Dr. Robert Lagani&egrave;re
* @version February 2001
* @see ocsftester.SimpleClient
*/
public class ClientFrame extends Frame
{

public Button getCloseB() {
	return closeB;
}

public void setCloseB(Button closeB) {
	this.closeB = closeB;
}

public Button getOpenB() {
	return openB;
}

public void setOpenB(Button openB) {
	this.openB = openB;
}

public Button getSendB() {
	return sendB;
}

public void setSendB(Button sendB) {
	this.sendB = sendB;
}

public Button getQuitB() {
	return quitB;
}

public void setQuitB(Button quitB) {
	this.quitB = quitB;
}

public TextField getPort() {
	return port;
}

public void setPort(TextField port) {
	this.port = port;
}

public TextField getHost() {
	return host;
}

public void setHost(TextField host) {
	this.host = host;
}

public TextField getMessage() {
	return message;
}

public void setMessage(TextField message) {
	this.message = message;
}

public Label getPortLB() {
	return portLB;
}

public void setPortLB(Label portLB) {
	this.portLB = portLB;
}

public Label getHostLB() {
	return hostLB;
}

public void setHostLB(Label hostLB) {
	this.hostLB = hostLB;
}

public Label getMessageLB() {
	return messageLB;
}

public void setMessageLB(Label messageLB) {
	this.messageLB = messageLB;
}

public List getListe() {
	return liste;
}

public void setListe(List liste) {
	this.liste = liste;
}

private MenuBar menuBar = new MenuBar();
private Menu ficMenu = new Menu("Options");
private MenuItem drawpadItem = new MenuItem("Drawpad");

private Menu sondage = new Menu("Sondages");
private MenuItem creer = new MenuItem("Créer un sondage");
private MenuItem repondre = new MenuItem("Répondre à un sondage");

private Button closeB =     new Button("Close");
private Button openB =      new Button("Open");
private Button sendB =      new Button("Send");
private Button quitB =      new Button("Quit");

private TextField port =    new TextField("12345");
private TextField host =    new TextField("localhost");
private TextField message = new TextField();
private Label portLB =      new Label("Port: ", Label.RIGHT);
private Label hostLB =      new Label("Host: ", Label.RIGHT);
private Label messageLB =   new Label("Message: ", Label.RIGHT);

private List liste =        new List();

private SimpleClient client;



// bady lst 
private List bady_liste1 =        new List();
private List bady_liste2 =        new List();
private Button ajouter = new Button("Add");
private Button supprimer = new Button("Delete");

public Button getSupprimer() {
	return supprimer;
}

public void setSupprimer(Button supprimer) {
	this.supprimer = supprimer;
}

public List getBady_liste1() {
	return bady_liste1;
}

public void setBady_liste1(List bady_liste1) {
	this.bady_liste1 = bady_liste1;
}

public List getBady_liste2() {
	return bady_liste2;
}

public void setBady_liste2(List bady_liste2) {
	this.bady_liste2 = bady_liste2;
}

public Button getAjouter() {
	return ajouter;
}

public void setAjouter(Button ajouter) {
	this.ajouter = ajouter;
}


public SimpleClient getClient() {
	return this.client;
}

public void setClient(SimpleClient client) {
	this.client = client;
}

public ClientFrame(String h, int p)
{
 super("OCSF Client");

 client = new SimpleClient(h, p, liste);
 port.setText(String.valueOf(p));
 host.setText(h);

 addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e)
   {
     quit();
   }
 });

 quitB.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e)
   {
     quit();
   }
 });

 closeB.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e)
   {
     close();
   }
 });

 openB.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e)
   {
     //open();
     Accueil acceuil = new Accueil("Connexion");
     EcouterFenetre fenetre = new EcouterFenetre(acceuil);
     EcouteurBouton bouton = new EcouteurBouton(acceuil,client);
     
     acceuil.inscription.addActionListener(bouton);
     acceuil.connexion.addActionListener(bouton);          
   }
 });

 sendB.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e)
   {
     send();
   }
 });

 Panel conteur_sud = new Panel();
 conteur_sud.setLayout(new GridLayout(5,2,5,5));

 conteur_sud.add(hostLB);
 conteur_sud.add(host);
 conteur_sud.add(portLB);
 conteur_sud.add(port);
 conteur_sud.add(messageLB);
 conteur_sud.add(message);
 conteur_sud.add(openB);
 conteur_sud.add(sendB);
 conteur_sud.add(closeB);
 conteur_sud.add(quitB);
 
 // test bady list
// List listeeee= new List();
 //listeeee.add("ABCD");
//listeeee.add("ABCE");
//this.setBady_liste1(listeeee);
UserList buddy = new UserList();
 UserList.recupusers();
 List listee= new List();
 listee=buddy.recupliste();
 this.setBady_liste1(listee);
 
 
 
 // conteneur ouest 
 Panel conteur_ouest = new Panel();
 conteur_ouest.add(bady_liste1);
 conteur_ouest.add(bady_liste2);
 conteur_ouest.add(supprimer);
 
 //conteneur est
 Panel conteur_est = new Panel();
 conteur_est.add(ajouter);
 
 // ecouteur boutons
 EcouteurButon ecouteurButonAdd = new EcouteurButon(this);
 this.ajouter.addActionListener(ecouteurButonAdd);
 this.supprimer.addActionListener(ecouteurButonAdd);
 
 
 setLayout(new BorderLayout(5,5));
 add("West", liste);
 add("South", conteur_sud);
 add("Center", conteur_ouest); 
 add("East", conteur_est); 
 
 // Menu Drawpad 
 ficMenu.add(drawpadItem);
 menuBar.add(ficMenu);
 

 EcouteurDrawPad ecouteurDrawPad = new EcouteurDrawPad(this);

 //Menu Sondage
  sondage.add(creer);
  sondage.add(repondre);
  menuBar.add(sondage);
  
  //TODO
 

 EcouteurCreer ecouteurCreer = new EcouteurCreer(this.client);
 EcouteurRepondre ecouteurRepondre = new EcouteurRepondre(this.client);
 
 drawpadItem.addActionListener(ecouteurDrawPad);
 creer.addActionListener(ecouteurCreer);
 repondre.addActionListener(ecouteurRepondre);

 setMenuBar(menuBar);
 
 setSize(350,450);
 setVisible(true);
}

private void readFields()
{
 int p = Integer.parseInt(port.getText());

 client.setPort(p);
 client.setHost(host.getText());
}

public void close()
{
 try {
   readFields();
   client.closeConnection();
 }
 catch (Exception ex)
 {
   liste.add(ex.toString());
   liste.makeVisible(liste.getItemCount()-1);
   liste.setBackground(Color.red);
 }
}

public void open()
{
 try {
   readFields();
   client.openConnection();
 }
 catch (Exception ex)
 {
   liste.add(ex.toString());
   liste.makeVisible(liste.getItemCount()-1);
   liste.setBackground(Color.red);
 }
}

public void send()
{
 try {
   readFields();
   client.sendToServer(message.getText());
 }
 catch (Exception ex)
 {
   liste.add(ex.toString());
   liste.makeVisible(liste.getItemCount()-1);
   liste.setBackground(Color.yellow);
 }
}

public void quit()
{
 System.exit(0);
}



}